/*
 * aclass_list.h
 *
 *  Created on: 2021��12��23��
 *      Author: taoye
 */

#ifndef SRC_COMM_ACLASS_ANALAYZE_ACLASS_LIST_H_
#define SRC_COMM_ACLASS_ANALAYZE_ACLASS_LIST_H_

#define PUB_LIST_VERSION    "V0.0.1"

/**
 * Single List structure
 */
struct slist_node
{
    struct slist_node *next;                         /**< point to next node. */
};
typedef struct slist_node slist_t;                /**< Type for single list. */

/**
 * @brief initialize a single list              初始化单个列表
 *
 * @param l the single list to be initialized
 */
static __inline void slist_init(slist_t *l)
{
    l->next = NULL;
}

static __inline void slist_append(slist_t *l, slist_t *n)
{
    struct slist_node *node;

    node = l;
    while (node->next) node = node->next;

    /* append the node to the tail 将节点附加到尾部 */
    node->next = n;
    n->next = NULL;
}

static __inline void slist_insert(slist_t *l, slist_t *n)
{
    n->next = l->next;
    l->next = n;
}

static __inline unsigned int slist_len(const slist_t *l)
{
    unsigned int len = 0;
    const slist_t *list = l->next;
    while (list != NULL)
    {
        list = list->next;
        len ++;
    }

    return len;
}

static __inline slist_t *slist_remove(slist_t *l, slist_t *n)
{
    /* remove slist head */
    struct slist_node *node = l;
    while (node->next && node->next != n) node = node->next;

    /* remove node */
    if (node->next != (slist_t *)0) node->next = node->next->next;

    return l;
}

static __inline slist_t *slist_first(slist_t *l)
{
    return l->next;
}

static __inline slist_t *slist_tail(slist_t *l)
{
    while (l->next) l = l->next;

    return l;
}

static __inline slist_t *slist_next(slist_t *n)
{
    return n->next;
}

static __inline int slist_isempty(slist_t *l)
{
    return l->next == NULL;
}

/**
 * rt_container_of - return the member address of ptr, if the type of ptr is the
 * struct type.
 */
#define container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))


/**
 * @brief get the struct for this single list node       获取此单个节点的结构
 * @param node the entry point
 * @param type the type of structure
 * @param member the name of list in structure
 */
#define slist_entry(node, type, member) \
    container_of(node, type, member)

/**
 * slist_for_each - iterate over a single list           遍历一个单链表
 * @pos:    the slist_t * to use as a loop cursor.
 * @head:   the head for your single list.
 */
#define slist_for_each(pos, head) \
    for (pos = (head)->next; pos != NULL; pos = pos->next)

/**
 * slist_for_each_entry  -   iterate over single list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:   the head for your single list.
 * @member: the name of the list_struct within the struct.
 */
#define slist_for_each_entry(pos_type, pos, head, member) \
    for (pos = slist_entry((head)->next, pos_type, member); \
         &pos->member != (NULL); \
         pos = slist_entry(pos->member.next, pos_type, member))

/**
 * slist_first_entry - get the first element from a slist
 * @ptr:    the slist head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the slist_struct within the struct.
 *
 * Note, that slist is expected to be not empty.
 */
#define slist_first_entry(ptr, type, member) \
    slist_entry((ptr)->next, type, member)

/**
 * slist_tail_entry - get the tail element from a slist
 * @ptr:    the slist head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the slist_struct within the struct.
 *
 * Note, that slist is expected to be not empty.
 */
#define slist_tail_entry(ptr, type, member) \
    slist_entry(slist_tail(ptr), type, member)



#endif /* SRC_COMM_ACLASS_ANALAYZE_ACLASS_LIST_H_ */
